package com.ruoyi.web.controller.spps.sup;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.web.controller.spps.util.ConnectionUtil;
import com.ruoyi.web.controller.spps.util.Pdf2String;
import com.ruoyi.web.controller.spps.util.Word2PdfUtil;

@RestController
@RequestMapping("/spps")
public class SppsController {
	@Autowired
	private ConnectionUtil util ;
	@Value("${spps.path}")
	private String path ;
	@Value("${spps.outPath}")
	private String outPath ;
	@RequestMapping("/string")
	public String getString(HttpServletResponse response,HttpServletRequest request){
		String result = "文件上传成功，请点击保存" ;
		response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域访问报错   
	    response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");   
	    response.setHeader("Access-Control-Max-Age", "3600"); //设置过期时间   
	    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, client_id, uuid, Authorization");   
	    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.   
	    response.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");
	    System.out.println("chenchao");
	    String id = request.getParameter("id");
	    String fileName = request.getParameter("fileName");
	    System.out.println(id);
	    String path = "D:\\software\\bea\\user_projects\\mydomain\\applications\\spps\\upload";
	    String filePath = path + fileName + ".doc";
	    String outPath = path + fileName + ".pdf"; 
	    try {
			Word2PdfUtil.doc2pdf(filePath, outPath);//word转pdf
			byte[] pdfBinary = Pdf2String.getPDFBinary(new File(outPath));//将pdf转成二进制
			if(pdfBinary == null ){//防止转二进制的时候出现异常
				result = "文件存在错误，请手动生成,并查看" ;
			}
		    result = util.insertBlob(pdfBinary, id,fileName);//将二进制存到数据库当中
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result = "文件上传出错" ;
			e.printStackTrace();
		}
	   
		return result;
	}
	@RequestMapping("/word/{fileName}")
	public String wordToPdf(@PathVariable(value="fileName") String fileName){
		
		return fileName;
	}
	@RequestMapping("/fileUpload")
	public String fileUpload (@RequestParam(value = "file", required = false)MultipartFile file,HttpServletRequest request,HttpServletResponse response)throws IOException{
		response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域访问报错   
	    response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");   
	    response.setHeader("Access-Control-Max-Age", "3600"); //设置过期时间   
	    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, client_id, uuid, Authorization");   
	    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.   
	    response.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");
		String filename = file.getOriginalFilename();
		String result = "文件上传成功，请点击保存" ;
		path = path + filename;
		outPath = path + filename.replace(".doc", "") + ".pdf";
		file.transferTo(new File(path));
		String id = request.getParameter("id");
		 try {
				Word2PdfUtil.doc2pdf(path, outPath);//word转pdf
				byte[] pdfBinary = Pdf2String.getPDFBinary(new File(outPath));//将pdf转成二进制
				if(pdfBinary == null ){//防止转二进制的时候出现异常
					result = "文件存在错误，请手动生成,并查看" ;
				}
			    result = util.insertBlob(pdfBinary, id,filename);//将二进制存到数据库当中
			} catch (Exception e) {
				// TODO Auto-generated catch block
				result = "文件上传出错" ;
				e.printStackTrace();
			}
		   	System.out.println("上传完成");
			return result;
	}
	@RequestMapping("/state")
	public String state(HttpServletRequest request,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域访问报错   
	    response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");   
	    response.setHeader("Access-Control-Max-Age", "3600"); //设置过期时间   
	    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, client_id, uuid, Authorization");   
	    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.   
	    response.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");
	    String id = request.getParameter("id");
	    String result=  "未上传" ;
	    try {
			result = util.fileuoloadState(id);
		} catch (Exception e) {
			result = "未上传" ;
			e.printStackTrace();
		}
	    return result ;
	}
}
